package com.service.business;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.service.business.rabbitmq.binding.GiftOrderSink;
import com.service.business.rabbitmq.binding.PointsOrderSink;
import com.service.business.rabbitmq.binding.WriteOffOrderSink;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2018</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019-04-01</dd>
 * </dl>
 */
@Slf4j
@EnableSwagger2
@EnableScheduling
@ServletComponentScan
@EnableSwaggerBootstrapUI
@EnableTransactionManagement
@MapperScan("com.service.*.mapper")
@ComponentScan("com.service.business.*")
@EnableBinding({ PointsOrderSink.class, WriteOffOrderSink.class})
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class BusinessApplication {

    public static void main(String[] args) {
        log.info("业务模块启动中");
        SpringApplication.run(BusinessApplication.class, args);
    }

    @Bean
    public ExecutorService executorService() {
        return new ScheduledThreadPoolExecutor(20,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
    }

    @Value("${swagger.enable}")
    private boolean enableSwagger;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enableSwagger)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.service.business"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("发现云api在线文档")
                .description("发现云APP前端业务相关")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}
